package com.zzxz.practicaltrainingsys.controller;

import com.zzxz.practicaltrainingsys.vo.ViewNotice;
import com.zzxz.practicaltrainingsys.entity.Notice;
import com.zzxz.practicaltrainingsys.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    //查询所有通知
    @GetMapping("/all")
    public List<Notice> queryAllNotice() {
        return noticeService.queryAllNotice();
    }

    //根据项目id查找通知
    @GetMapping("/find")
    public List<Notice> getNoticeById(Integer id) {
        return noticeService.getNoticeById(id);
    }

    // 根据通知ID获取详情
    @GetMapping("/detail")
    public ViewNotice getNoticeDetailById(Integer id) {
        return noticeService.getNoticeDetailById(id);
    }

    // 删除通知根据通知ID
    @GetMapping("/delete")
    public int deleteNotice(Integer id) {
        return noticeService.deleteNoticeById(id);
    }

    // 创建插入通知 - 修复时间处理和参数验证
    @PostMapping("/insert")
    public Map<String, Object> insertNotice(@RequestBody Notice notice) {
        Map<String, Object> result = new HashMap<>();

        try {
            System.out.println("接收到的通知对象: " + notice);

            // 参数验证
            if (notice.getNotice_name() == null || notice.getNotice_name().trim().isEmpty()) {
                result.put("code", 400);
                result.put("message", "通知名称不能为空");
                return result;
            }

            if (notice.getNotice_content() == null || notice.getNotice_content().trim().isEmpty()) {
                result.put("code", 400);
                result.put("message", "通知内容不能为空");
                return result;
            }

            if (notice.getProject_id() == null) {
                result.put("code", 400);
                result.put("message", "项目ID不能为空");
                return result;
            }

            if (notice.getTeacher_id() == null) {
                result.put("code", 400);
                result.put("message", "教师ID不能为空");
                return result;
            }

            // 如果前端没有传递时间，则使用当前时间
            if (notice.getNotice_time() == null) {
                notice.setNotice_time(new Date());
                System.out.println("使用当前时间: " + notice.getNotice_time());
            } else {
                System.out.println("使用传入时间: " + notice.getNotice_time());
            }

            int insertResult = noticeService.insertNotice(notice);

            if (insertResult > 0) {
                result.put("code", 200);
                result.put("message", "通知创建成功");
                result.put("data", insertResult);
            } else {
                result.put("code", 500);
                result.put("message", "通知创建失败");
            }

        } catch (Exception e) {
            System.err.println("创建通知时发生错误: " + e.getMessage());
            e.printStackTrace();
            result.put("code", 500);
            result.put("message", "服务器内部错误: " + e.getMessage());
        }

        return result;
    }

    //分页查询信息
    @GetMapping("/admnotice")
    public Map<String, Object> getAllNotices(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {

        List<ViewNotice> list = noticeService.getAllNoticesByPage(pageNum, pageSize);
        int total = noticeService.getTotalNoticeCount();

        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        return result;
    }

    // 根据学生ID分页查询该学生参与项目的所有通知
    @GetMapping("/student/{studentId}")
    public Map<String, Object> getNoticesByStudentId(
            @PathVariable Integer studentId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {

        List<ViewNotice> list = noticeService.getNoticesByStudentId(studentId, pageNum, pageSize);
        int total = noticeService.getTotalNoticeCountByStudentId(studentId);

        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        return result;
    }

    @GetMapping("/page2")
    public Map<String, Object> getNoticePage2(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Integer project_id) {
        int offset = (pageNum - 1) * pageSize;
        List<ViewNotice> list = noticeService.selectNoticePage(offset, pageSize, project_id);
        int total = noticeService.selectNoticePageTotal(project_id);

        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        return result;
    }

    // 新增：根据项目ID查询该项目的所有通知（供学生端使用）
    @GetMapping("/project")
    public List<ViewNotice> getNoticesByProjectId(@RequestParam Integer project_id) {
        return noticeService.getNoticesByProjectId(project_id);
    }
}